// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;
using SnapObjects.Expressions;

namespace SnapObjects.Data
{
    internal class TemplateExportMetadataProvider : ITemplateExportMetadataProvider
    {
        public TemplateExportMetadataProvider()
        {
            this.Datas = new Dictionary<string, Metadata<EvaluateResult>>();
        }

        public MetadataType Type => MetadataType.Parameter;

        public string[] SupportKinds => new string[] { SourceKind.Default };

        private IDictionary<string, Metadata<EvaluateResult>> Datas { get; }

        public bool TryGet(object key, ParserOptions<EvaluateResult> options, out Metadata<EvaluateResult> metadata)
        {
            if (key is string name)
            {
                if (this.Datas.TryGetValue(name, out var meta))
                {
                    metadata = meta;

                    return true;
                }
            }

            metadata = null;

            return false;
        }

        public void AddParameter(string name, object value)
        {
            if (this.Datas.ContainsKey(name))
            {
                this.Datas.Remove(name);
            }

            var metaData = new ParameterMetadata<EvaluateResult>(
                name, typeof(object), new EvaluateResult(value));

            this.Datas.Add(name, metaData);
        }
    }
}
